package com.orhon.smartcampus.modules.student.fetcher;//package com.orhon.smartcampus.modules.student.fetcher;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.SInformationDao;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class StudentByIdsFetcher implements DataFetcher  {

	@Autowired
	SInformationDao sInformationDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		List<SInformation> students=new ArrayList<SInformation>();
		Long schoolId = JwtUtils.getSchoolId(token);
		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			String key = map.get("ids").toString();
			String substring = key.substring(1, key.length() - 1);
			String[] split = substring.split(",");
			for (String id : split) {
				String hql = "from SInformation where deleted_at = null and id = "+id;//+" and school_id = " + schoolId;
				SInformation sInformation = sInformationDao.findId(hql, null);
				students.add(sInformation);
			}
		}
		List<GQLStudent> student =  StudentMapper.MAPPER.toStudentList(students);
		return Result.ok().put("data", student);

	}}
